﻿using furion.blog.Application.Contracts;
using furion.blog.Domain.Query.Message;
using furion.blog.Domain.Shared.Attribute;
using furion.blog.Domain.Shared.Dto.Message;
using furion.blog.Domain.Shared.Enum;
using furion.blog.Utils;
using Microsoft.AspNetCore.Mvc;

namespace furion.blog.Web.Entry.Api
{
    /// <summary>
    /// 站内信息接口
    /// </summary>
    [ApiDescriptionSettings(Groups = new string[] { "MessageApi" })]
    [Route("api/message")]
    public class MessageApiController
    {
        private readonly IMessageService _messageService;
        public MessageApiController(IMessageService messageService)
        {
            this._messageService = messageService;
        }


        /*********************后台接口*****************************************************/

        [ActionAttribute(Name = "我的站内信息分页接口", Desc = "获取当前会员站内信息")]
        [HttpGet, Route("list")]
        public async Task<IActionResult> list(
            [FromQuery(Name = "page")] int pageIndex,
            [FromQuery(Name = "limit")] int pageSize,
            [FromQuery(Name = "isOpen")] string isOpen = "",
            [FromQuery(Name = "isDelete")] string isDelete = "",
            [FromQuery(Name = "date")] string date = "",
            [FromQuery(Name = "toUser")] long? toUser = null,
            [FromQuery(Name = "searchContent")] string searchContent = ""
            )
        {
            var ret = await this._messageService.List(new MessageListQuery
            {
                PageSize = pageSize,
                PageIndex = pageIndex,
                IsDelete = YesOrNoEnum.No.ToString(),
                IsOpen = isOpen,
                Date = date,
                ToUser = toUser,
                SearchContent = searchContent,
            });
            return new OkObjectResult(ResultUtil.Success(ret));
        }

        /*********************app接口*****************************************************/
        [ActionAttribute(Name = "信息数量统计接口", Desc = "获取用户信息数量")]
        [HttpGet, Route("count")]
        public async Task<IActionResult> updateProfile([FromQuery(Name = "isOpen")] string isOpen,
            [FromQuery(Name = "isDelete")] string isDelete,
            [FromHeader(Name = "MemberId")] long memberId)
        {
            var ret = await this._messageService.Count(new MessageDetailQuery
            {
                ToUser = memberId,
                IsDelete = isDelete,
                IsOpen = isOpen
            });
            return new OkObjectResult(ResultUtil.Success(ret));
        }


        [ActionAttribute(Name = "我的站内信息分页接口", Desc = "获取当前会员站内信息")]
        [HttpGet, Route("mylist")]
        public async Task<IActionResult> mylist(
            [FromQuery(Name = "page")] int pageIndex,
            [FromQuery(Name = "limit")] int pageSize,
                        [FromHeader(Name = "MemberId")] long memberId,
            [FromQuery(Name = "isOpen")] string isOpen = "No"
            )
        {
            var ret = await this._messageService.MyList(new MessageListQuery
            {
                PageSize = pageSize,
                PageIndex = pageIndex,
                IsDelete = YesOrNoEnum.No.ToString(),
                IsOpen = isOpen,
                ToUser = memberId,
            });
            return new OkObjectResult(ResultUtil.Success(ret));
        }

        [ActionAttribute(Name = "设置信息读取接口", Desc = "设置信息为已读取")]
        [HttpPost, Route("open/{id}")]
        public async Task<IActionResult> open([FromRoute(Name = "id")] long messageId, [FromHeader(Name = "MemberId")] long memberId)
        {
            var ret = await this._messageService.Open(messageId, memberId);
            return new OkObjectResult(ResultUtil.Success());
        }

        [ActionAttribute(Name = "信息回复接口", Desc = "回复信息")]
        [HttpPost, Route("repeat")]
        public async Task<IActionResult> repeat([FromBody] MessageRepeatDto dto, [FromHeader(Name = "MemberId")] long memberId)
        {
            dto.FromUser = memberId;
            var ret = await this._messageService.Repeat(dto);
            return new OkObjectResult(ResultUtil.Success(ret));
        }

        [ActionAttribute(Name = "信息删除接口", Desc = "删除自己的信息")]
        [HttpDelete, Route("{id}")]
        public async Task<IActionResult> del([FromRoute(Name = "id")]long id,[FromHeader(Name = "MemberId")]long memberId) {
            var ret = await this._messageService.DelBySelf(id,memberId);
            return new OkObjectResult(ResultUtil.Success(ret));
        }

    }
}
